package com.wang.springframework.beans.factory.support;

import com.wang.springframework.beans.BeansException;
import com.wang.springframework.beans.factory.BeanFactory;
import com.wang.springframework.beans.factory.config.BeanDefinition;

/**
 * @author wangjianhua
 * @Description
 * @date 2021/8/23/023 11:22
 **/
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String name) throws BeansException {
        //有则返回
        Object bean = getSingleton(name);
        if(bean != null){
            return bean;
        }
        //没有就创建
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name,beanDefinition);
    }

    /**
     * 获取bean定义
     * @param beanName beanName
     * @return bean定义
     * @throws BeansException 异常
     */
    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    /**
     * 创建bean
     * @param beanName beanName
     * @param beanDefinition bean定义
     * @return 实体
     * @throws BeansException 异常
     */
    protected abstract Object createBean(String beanName,BeanDefinition beanDefinition) throws BeansException;
}
